<template>
  <el-form style="width: 60%" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="学生姓名" prop="Sname">
      <el-input v-model="ruleForm.Sname"></el-input>
    </el-form-item>
    <el-form-item label="报名时间" prop="Sdate">
      <el-input v-model="ruleForm.Sdate"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="Spassword">
      <el-input v-model="ruleForm.Spassword"></el-input>
    </el-form-item>
    <el-form-item label="交款额" prop="Spayment">
      <el-input v-model="ruleForm.Spayment"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        Sname: '',
        Sdate: '',
        Spassword: '',
        Spayment: '',
      },
      rules: {
        Sname: [
          { required: true, message: '学生名字不能为空', trigger: 'blur' }
        ],
        Sdate: [
          { required: true, message: '报名时间不能为空', trigger: 'blur' }
        ],
        Spassword: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        Spayment: [
          { required: true, message: '交款额不能为空', trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      const _this = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post('http://localhost:8152/api/auth/student/add',this.ruleForm).then(function (res) {
            console.log(res.data)
            if(res.data.msg == "添加成功"){
              _this.$alert(_this.ruleForm.Sname+'添加成功！', '消息', {
                confirmButtonText: '确定',
                callback: action => {
                  _this.$router.push('/studentInformation')
                }
              })
            } else if (res.data.msg == '学生已经存在') {
              alert("添加失败,学生已经存在!")
            }
          })
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
